package com.aishua.aishuawallet.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * Created by aishua on 2018/9/1.
 */

public class TimeUtil {


    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    /**
     * 返回文字描述的日期
     *
     * @param timeStamp
     * @return
     */
    public static String getTimeFormatText(String timeStamp) {

       Date date  =new Date(Long.valueOf(timeStamp+"000"));
        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - date.getTime();
        long r = 0;
//        if (diff > year) {
//            r = (diff / year);
//            return r + "年前";;
//        }
        if (diff > month) {
//            r = (diff / month);

            return timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss");
        }
        if (diff > day) {

            r = (diff / day);

            if (r > 3){

                return timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss");
            }
            return r + "天前";
        }

        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }


    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }



}
